﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace StoryFlow
{
    public class StoryProject
    {
        // 项目是否保存
        private bool _isSaved = true;
        public bool IsSaved { set { _isSaved = value; } get => _isSaved; }
        private void NotSaved()
        {
            _isSaved = false;
        }
        public void Saved()
        {
            _isSaved = true;
        }

        // 项目路径，不会被保存到项目
        private string _projectPath = "";
        public string ProjectPath
        {
            set { _projectPath = value; }
            get => _projectPath;
        }

        #region 序列化内容

        // 项目的基本信息
        // 故事的标题
        private string _title;
        public string Title { set { _title = value; NotSaved(); } get => _title; }

        // 故事简介
        private string _description;
        public string Description { set { _description = value; NotSaved(); } get => _description; }

        // 故事的作者
        private string _author;
        public string Author { set { _author = value; NotSaved(); } get => _author; }

        // 故事的类别
        private string _category;
        public string Category { set { _category = value; NotSaved(); } get => _category; }

        #endregion

        // 序列化为XML
        public XmlDocument SerializeToXML()
        {
            XmlDocument proj = new XmlDocument();
            XmlDeclaration xmlDecl = proj.CreateXmlDeclaration("1.0", "utf-8", null);
            proj.AppendChild(xmlDecl);

            // 工程文件的根节点
            XmlElement root = proj.CreateElement("Project");
            proj.AppendChild(root);

            // 根->基础信息
            XmlElement basicInfo = proj.CreateElement("BasicInfo");
            root.AppendChild(basicInfo);

            // 根->基础信息->标题
            XmlElement title = proj.CreateElement("title");
            title.InnerText = Title;

            basicInfo.AppendChild(title);

            // 根->基础信息->简介
            XmlElement description = proj.CreateElement("description");
            description.InnerText = Description;
            basicInfo.AppendChild(description);

            // 根->基础信息->作者
            XmlElement author = proj.CreateElement("author");
            author.InnerText = Author;
            basicInfo.AppendChild(author);

            // 根->基础信息->分类
            XmlElement category = proj.CreateElement("category");
            category.InnerText = Category;
            basicInfo.AppendChild(category);

            return proj;
        }

        // 从XML文件反序列化
        public bool LoadFromXML(XmlDocument xmlDocument)
        {
            bool success = true;
            XmlNode basicInfo = xmlDocument.SelectSingleNode("Project/BasicInfo");
            XmlNodeList basicInfoList = basicInfo.ChildNodes;

            foreach (XmlNode basicInfoNode in basicInfoList)
            {
                if (basicInfoNode.Name == "title")
                {
                    Common.currentProject.Title = basicInfoNode.InnerText;
                }
                else if (basicInfoNode.Name == "description")
                {
                    Common.currentProject.Description = basicInfoNode.InnerText;
                }
                else if (basicInfoNode.Name == "author")
                {
                    Common.currentProject.Author = basicInfoNode.InnerText;
                }
                else if (basicInfoNode.Name == "category")
                {
                    Common.currentProject.Category = basicInfoNode.InnerText;
                }
            }
            return success;
        }
    }
}
